import * as Assets from '../assets';

export default class Intro extends Phaser.State {
    content: Array<string> = [
        '你好,这是一封简历',
        '',
        '故事从2009年开始，和大多数人一样，那一年我开始走',
        '入社会，尝试创造价值',
    ];
    text: Phaser.Text;

    create() {
        this.text = this.game.add.text(32, 32, '', { font: '30px Simhei', fill: '#FFFFFF' });
        this.showText();
    }

    showText() {

        this.text.setShadow(3, 3, 'rgba(255,255,255,0.5)', 2);

        this.nextLine();
    }
    lineIndex: number = 0;
    wordIndex: number = 0;
    line: Array<string>;
    nextLine() {
        if (this.lineIndex === this.content.length) {
            // all Finished
            this.showOther(() => {
                this.game.time.events.add(1000, () => {
                    // all finish
                    this.game.state.clearCurrentState();
                    this.game.state.start('world');
                });
            });
            return;
        }
        this.line = this.content[this.lineIndex].split('');
        this.wordIndex = 0;
        this.game.time.events.repeat(120, this.line.length, this.nextWord, this);
        this.lineIndex += 1;
    }
    nextWord(text: Phaser.Text) {
        if (this.line.length !== 0) {
            this.text.text = this.text.text.concat(this.line[this.wordIndex]);
            this.wordIndex += 1;
        }
        if (this.wordIndex === this.line.length) {
            this.game.time.events.add(100, this.nextLine, this);
            this.text.text = this.text.text.concat('\n');
        }
    }
    showOther(showFinish: Function) {
        const iconDufu: Phaser.Sprite = this.game.add.sprite(this.game.world.width, this.game.world.height, Assets.Images.SpritesheetsBz.getName());
        const icon1: Phaser.Sprite = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, Assets.Images.SpritesheetsBz1.getName());
        iconDufu.anchor.setTo(1, 0);

        icon1.anchor.setTo(0.5, 0.5);
        icon1.alpha = 0;
        icon1.scale.setTo(2, 2);

        const otherTween1: Phaser.Tween = this.game.add.tween(iconDufu).to({ y: this.game.world.height - iconDufu.height - 1 },
            600, Phaser.Easing.Cubic.In, true, 1000);
        otherTween1.onComplete.add(showFinish);

        // const otherTween2: Phaser.Tween = this.game.add.tween(icon1).to({scale:{x: 1, y: 1}, alpha: 1}, 600, Phaser.Easing.Bounce.In, true);
        const otherTween2: Phaser.Tween = this.game.add.tween(icon1).to({ alpha: 1 }, 600, Phaser.Easing.Linear.None, true);
        this.game.add.tween(icon1.scale).to({ x: 1, y: 1 }, 600, Phaser.Easing.Bounce.Out, true);
    }
}
